<template>
  <div class="images-view">
    <div
      class="images-item"
      v-for="(item, index) in attachments"
      :key="index"
      @click="showPreviewFile(item)"
    >
      <img class="image" :src="'/api/file/download/' + item.fileId" />
      <div class="close" v-if="!readonly" @click="removeItem(item)">
        <CloseOutlined />
      </div>
    </div>
    <div class="images-item add" v-if="!readonly">
      <a-upload
        v-model:file-list="fileList"
        :multiple="true"
        name="file"
        :showUploadList="false"
        :customRequest="uploadRequest"
        @change="(e) => onUploadChanged(e)"
      >
        <div class="images-upload">
          <UploadOutlined />
        </div>
      </a-upload>
    </div>
  </div>
  <preview-file
    v-model:visible="previewModel.visible"
    :id="previewModel.id"
    :fileName="previewModel.fileName"
    v-if="previewModel.visible"
  />
</template>

<script lang="ts" setup>
import { AttachmentItemDto } from "@/api/models";
import { computed, onMounted, ref, watch } from "vue";
import { useDataStore } from "@/store";
import configApi from "@/api/resource/attachmentConfig";
import uploadApi from "@/api/resource/fileResource";
import { storeToRefs } from "pinia";
import { UploadFileResourceInputDto } from "@/api/resource/types";
import { UploadOutlined, CloseOutlined } from "@ant-design/icons-vue";
import type { UploadChangeParam } from "ant-design-vue";
import PreviewFile from "@/components/preview-file/Index.vue";

const dataStore = useDataStore();
const { attachmentConfigItems } = storeToRefs(dataStore);

/**
 * 附件管理输入框
 */
type AttachmentInputProp = {
  bucketName: string;
  dir?: string;
  bussinessType: string;
  readonly: boolean;
  groupName?: string;
  attachments: Array<AttachmentItemDto>;
};

const props = withDefaults(defineProps<AttachmentInputProp>(), {
  readonly: false,
});

const fileList = ref([[]]);

const previewModel = ref<{ visible: boolean; id: string; fileName: string }>({
  id: "",
  fileName: "",
  visible: false,
});

const uploadRequest = (options: any) => {
  let data: UploadFileResourceInputDto = {
    dir: props.dir,
    bucketName: props.bucketName || "default",
    fileName: options.file.name,
    isPublic: false,
    file: options.file,
  };
  uploadApi.upload(data).then((res) => {
    options.onSuccess(res, options.file);
  });
};

const uploading = ref(false);

const onUploadChanged = (info: UploadChangeParam) => {
  uploading.value = info.file.status == "uploading";
  if (info.file.status === "done") {
    //上传成功
    let res = info.file.response.data;
    props.attachments.push({
      fileId: res.id,
      fileType: res.fileType,
      name: res.fileName,
      groupName: props.groupName || "",
      extraProperties: {},
    });
  }
};

const removeItem = (item: AttachmentItemDto) => {
  let index = props.attachments.findIndex((x) => x == item);
  if (index !== -1) {
    props.attachments.splice(index, 1);
  }
};

const showPreviewFile = (item: AttachmentItemDto) => {
  previewModel.value = {
    visible: true,
    id: item.fileId,
    fileName: item.name,
  };
};

onMounted(async () => {
  if (!attachmentConfigItems.value.length) {
    let items = await configApi.getAllList({});
    dataStore.setAllAttachmentConfigItems(items);
  }
});
</script>

<style lang="less" scoped>
.images {
  &-view {
    display: flex;
    flex-wrap: wrap;
  }

  &-item {
    margin-left: 5px;
    margin-top: 5px;
    background: #f9f9f9;
    border: 1px solid #eee;
    border-radius: 5px;
    overflow: hidden;
    width: 80px;
    height: 80px;
    position: relative;

    &.add {
      padding: 0;
      &:hover {
        background: transparent;
      }
    }

    &:hover {
      background: #0081ff;
    }

    .image {
      width: 100%;
      height: 100%;
    }
    .close {
      position: absolute;
      z-index: 1;
      right: 0;
      top: 0;
      padding: 3px 8px;
      background: #ff330022;
      color: #777;

      &:hover {
        background: #ff3300;
        color: #fff;
      }
    }
  }

  &-upload {
    width: 80px;
    height: 80px;
    display: flex;
    color: #999;
    font-size: 24px;
    align-items: center;
    justify-content: center;

    &:hover {
      background: #0081ff66 !important;
      color: #fff;
    }
  }
}
</style>
